package org.jeecg.config.mybatis;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import org.jeecg.common.util.oConvertUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Configuration
@MapperScan({"org.jeecg.modules.**.mapper*","com.yhy.wxzn.**.mapper*"})
public class MybatisPlusSaasConfig {
    private static final String TENANT_FIELD_NAME = "tenant_id";
    private static final List<String> tenantTable = new ArrayList();

    public MybatisPlusSaasConfig() {
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() {
            public Expression getTenantId() {
                String tenant_id = oConvertUtils.getString(TenantContext.getTenant(), "0");
                return new LongValue(tenant_id);
            }

            public String getTenantIdColumn() {
                return "tenant_id";
            }

            public boolean ignoreTable(String tableName) {
                Iterator var2 = MybatisPlusSaasConfig.tenantTable.iterator();

                String temp;
                do {
                    if (!var2.hasNext()) {
                        return true;
                    }

                    temp = (String)var2.next();
                } while(!temp.equalsIgnoreCase(tableName));

                return false;
            }
        }));
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }

    static {
        tenantTable.add("demo");
    }
}
